package com.example.wechat.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import com.example.wechat.R;
import com.example.wechat.activity.ContactDetailActivity;
import com.example.wechat.adapter.ContactAdapter;
import com.example.wechat.model.Contact;

public class ContactFragment extends Fragment implements ContactAdapter.OnItemClickListener {
    private SearchView searchView;
    private RecyclerView recyclerView;
    private ContactAdapter adapter;
    private List<Contact> contacts;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact, container, false);
        initViews(view);
        loadContacts();
        return view;
    }

    private void initViews(View view) {
        searchView = view.findViewById(R.id.searchView);
        recyclerView = view.findViewById(R.id.recyclerView);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                filterContacts(newText);
                return true;
            }
        });

        setupRecyclerView();
    }

    private void setupRecyclerView() {
        contacts = new ArrayList<>();
        adapter = new ContactAdapter(contacts, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
    }

    private void loadContacts() {
        // 这里应该从数据库加载联系人列表
        // 临时使用测试数据
        contacts.clear();
        contacts.add(new Contact("1", "张三", "https://example.com/avatar1.jpg"));
        contacts.add(new Contact("2", "李四", "https://example.com/avatar2.jpg"));
        contacts.add(new Contact("3", "王五", "https://example.com/avatar3.jpg"));
        adapter.notifyDataSetChanged();
    }

    private void filterContacts(String query) {
        if (query.isEmpty()) {
            loadContacts();
            return;
        }

        List<Contact> filteredContacts = new ArrayList<>();
        for (Contact contact : contacts) {
            if (contact.getName().toLowerCase().contains(query.toLowerCase()) ||
                (contact.getRemark() != null && contact.getRemark().toLowerCase().contains(query.toLowerCase()))) {
                filteredContacts.add(contact);
            }
        }

        adapter.updateContacts(filteredContacts);
    }

    @Override
    public void onResume() {
        super.onResume();
        loadContacts(); // 每次返回时刷新联系人列表
    }

    @Override
    public void onItemClick(Contact contact, int position) {
        Intent intent = new Intent(getActivity(), ContactDetailActivity.class);
        intent.putExtra("contact_id", contact.getId());
        startActivity(intent);
    }
} 